import { Component, Injectable, Injector, Input } from '@angular/core';
import { UniversalFunctionsUtil } from '../../../core/universal-functions.util';
import * as _ from 'lodash';

@Injectable({ providedIn: 'root' })

@Component({
    selector: 'app-repairform',
    templateUrl: './repairForm.component.html',
    styles: [`
        form {
            .tac {
                text-align: center;
            }        
            .h3 {
                font-size: 26px;
                font-weight: 700;
            }        
        }
    `]
})
export class RepairFormComponent extends UniversalFunctionsUtil {
    @Input() apiIdentity;
    @Input() btnType;
    @Input() queryId;
    @Input() readOnly;
    fileName = '高速铁路临时要点维修申请表.doc';
    no = '';
    content = `登记站：XX站
1.XX年XX月XX日昆明南电务段发现XX站XX设备XX，特申请XX年XX月XX日XX时XX分至XX月XX日XX时XX分紧急修对XX站XX设备进行检查试验作业；
2.计划完成XX设备检查试验X站(区间)；
3.影响XX站全站及XX站-XX站-XX站间上下行线信号设备使用；
4.作业人员携带工具材料由XX、XX作业门进出；
5.此项作业在车站操作方式下进行`;
    workOrg = `昆明南电务段`;
    helpOrg = `XX段 XX段`;
    applyOrg = '';
    applyUser = '';
    phone = '';

    showModal() {
        this.isVisible = true;
        const queryId = this.queryId;
        if (queryId) {
            this.submitting = true;
            this.apiService.scanRepairFile({ id: queryId }).subscribe(res => {
                if (!_.isEmpty(res)) {
                    this.no = res.no;
                    this.content = res.content || '';
                    this.workOrg = res.workOrg || '';
                    this.helpOrg = res.helpOrg || '';
                    this.applyOrg = res.applyOrg || '';
                    this.applyUser = res.applyUser || '';
                    this.phone = res.phone || '';
                    this.submitting = false;
                }
            }, err => this.errorHandle(err));
        }
    }

    submitForm() {
        this.submitting = true;
        const no = this.no, content = this.content, workOrg = this.workOrg, helpOrg = this.helpOrg,
            applyOrg = this.applyOrg, applyUser = this.applyUser, phone = this.phone,
            params: any = { no, content, workOrg, helpOrg, applyOrg, applyUser, phone };
        this.btnType == 'default' && this.queryId ? params.id = this.queryId : params.id = '';

        const formData = new FormData();
        formData.append('applyOrg', params.applyOrg);
        formData.append('applyUser', params.applyUser);
        formData.append('content', params.content);
        formData.append('helpOrg', params.helpOrg);
        formData.append('applyOrg', params.applyOrg);
        formData.append('no', params.no);
        formData.append('phone', params.phone);
        formData.append('workOrg', params.workOrg);
        
        this.apiService.tempRepairForm(formData).subscribe(res => {
            this.remoteMessage(res);
            this.ums.sendMessage('repairform' as string);
            this.submitting = this.isVisible = false;
        }, err => this.errorHandle(err));
    }

    constructor(private injector: Injector) {
        super(injector);
    }
}

